package com.dada.job;

import com.dangdang.ddframe.job.api.config.JobConfiguration;
import com.dangdang.ddframe.job.spring.namespace.parser.common.AbstractJobConfigurationDto;
import com.dangdang.ddframe.job.spring.schedule.SpringJobScheduler;
import com.dangdang.ddframe.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import static com.dada.common.JobConfigurationFactory.simpleJobConfigBuilder;

/**
        *集中配置定时任务
        *Created by Jearton on 16/7/4.
        */
@Configuration
@Profile({"dev", "test", "prod"})
@ConditionalOnExpression("'${elastic.job.server-lists}'.length() > 0")
@EnableConfigurationProperties(JobCrons.class)
public class JobConfig {

    @Autowired
    private ZookeeperRegistryCenter regCenter;

    @Autowired
    private JobCrons jobCrons;

    /**
     * 订单变更通知
     */
    @Bean(initMethod = "init")
    public SpringJobScheduler orderNotifyJobScheduler() {
        Class<TaskDemo> clazz = TaskDemo.class;
        String cron = jobCrons.getTaskNotify();
        JobConfiguration jobConfig = simpleJobConfigBuilder(clazz, 1, cron).overwrite(true).build();
        return new SpringJobScheduler(regCenter, (AbstractJobConfigurationDto) jobConfig);
    }


}